﻿/*---------------------------------------------------------------------*/
// fileName="ThreadCommon.cs" CompanyName="SureKAM"
// Copyright SureKAM (c). All right reserved.
/*---------------------------------------------------------------------*/

using System.Threading;
using SureKAM.BackUp.Notify.Domain;

namespace SureKAM.BackUp.Notify.Common
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;

    public class ThreadCommon
    {
        #region 私有字段
        private static List<Thread> thread=new List<Thread>();
        private static List<ThreadPackCommon> watchCommon = new List<ThreadPackCommon>();
        private static List<Domain.Directory> directories=new List<Directory>();
        private const int MaxThreadCount=40;
        #endregion

        #region 构造
        public ThreadCommon()
        {
        }
        #endregion

        #region 私有方法
        #endregion

        #region 属性
        /// <summary>
        /// 最大线程数
        /// </summary>
        public static int MaxCount
        {
            get { return MaxThreadCount; }
        }

        /// <summary>
        /// 当前线程数
        /// </summary>
        public static int Count
        {
            get { return thread.Count; }
        }

        /// <summary>
        /// 线程池
        /// </summary>
        public static List<Thread> Threads
        {
            get { return thread; }
            set { thread = value; }
        }

        /// <summary>
        /// 目录
        /// </summary>
        public static List<Domain.Directory> Directories
        {
            get { return directories; }
        }
        #endregion

        #region 公有方法
        /// <summary>
        /// 创建一个线程
        /// </summary>
        /// <param name="directory">目录对象</param>
        /// <returns>返回线程</returns>
        public static Thread CreateThread(Domain.Directory directory)
        {
            Thread currentThread=null;
            if(thread.Count<MaxThreadCount)
            {
                ThreadPackCommon threadPackCommon=new ThreadPackCommon(directory);
                watchCommon.Add(threadPackCommon);
                directories.Add(threadPackCommon.Direct);
                currentThread = new Thread(new ThreadStart(threadPackCommon.Start));
                thread.Add(currentThread);
                currentThread.Start();
            }
            return currentThread;
        }

        /// <summary>
        /// 中止线程
        /// </summary>
        public static void StopThread()
        {
            if(thread.Count>0)
            {
                try
                {
                    foreach (ThreadPackCommon threadPackCommon in watchCommon)
                    {
                        threadPackCommon.Stop();
                    }
                    watchCommon = new List<ThreadPackCommon>();
                    foreach (Thread thread1 in thread)
                    {
                        thread1.Abort();
                    }
                }
                catch (Exception)
                {
                }
            }
        }
        #endregion
    }
}
